package com.lovo.controller;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.lovo.model.Commodity;
import com.lovo.service.CommodityService;
import com.lovo.service.impl.CommodityServiceImpl;
import com.lovo.util.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author:kyle
 * @Date:2020/11/26 - 14:13
 * @Project:pet_sys
 * @Package:${PACKAGE_NAME}
 * @Version:1.0
 */
@WebServlet(name = "CommodityServlet",urlPatterns = "/commodity.do")
public class CommodityServlet extends HttpServlet {

    CommodityService cs = new CommodityServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");

        if(method == null || method.equals("")){
            IndexByPage(request,response);
        }else if(method.equalsIgnoreCase("insertCommodity")){
            insertCommodity(request,response);
        }else if(method.equalsIgnoreCase("deleteCommodity")){
            deleteCommodity(request,response);
        }else if(method.equalsIgnoreCase("updateCommodity")){
            updateCommodity(request,response);
        }else if(method.equalsIgnoreCase("getCommodityById")){
            getCommodityById(request,response);
        }
    }

    // 主表
    protected void IndexByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pn = request.getParameter("page");
        int pageNo = 1;
        if(pn != null){
            pageNo = Integer.parseInt(pn);
        }
        String ps = request.getParameter("maxResult");
        int pageSize = 1;
        if(ps != null){
            pageSize = Integer.parseInt(ps);
        }

        String commodityName = request.getParameter("commodityName");
        String commodityType = request.getParameter("commodityType");

        PageInfo<Commodity> pageInfo = cs.listsBySelective(pageNo,pageSize,commodityName,commodityType);
        Gson json = new Gson();
        String toJson = json.toJson(pageInfo);
        response.getWriter().write(toJson);
    }

    // 删除商品
    protected void deleteCommodity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("commodityId");
        Integer commodityId = null;
        if(id != null) {
            commodityId = Integer.parseInt(id);
        }
        boolean flag = cs.deleteByPrimaryKey(commodityId);
        response.getWriter().write(String.valueOf(flag));
    }

    // 添加商品
    protected void insertCommodity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String commodityName = request.getParameter("commodityName");
        String commodityType = request.getParameter("commodityType");
        String commodityMaterial = request.getParameter("commodityMaterial");
        String commodityWeightStandard = request.getParameter("commodityWeightStandard");
        String commodityStandard = request.getParameter("commodityStandard");
        String commodityPackage = request.getParameter("commodityPackage");
        String commodityTaste = request.getParameter("commodityTaste");
        String commoditySpecial = request.getParameter("commoditySpecial");
        String price = request.getParameter("commodityPrice");
        Integer commodityPrice = null;
        if(price != null){
            commodityPrice = Integer.parseInt(price);
        }
        String commodityImages = request.getParameter("commodityImages");
        String commodityOrigin = request.getParameter("commodityOrigin");
        String commodityFactoryTime = request.getParameter("commodityFactoryTime");
        String commodityShelf = request.getParameter("commodityShelf");
        String commoditySpecialMsg = request.getParameter("commoditySpecialMsg");




        Commodity c = new Commodity(commodityName,
                                    commodityType,
                                    commodityMaterial,
                                    commodityWeightStandard,
                                    commodityStandard,
                                    commodityPackage,
                                    commodityTaste,
                                    commoditySpecial,
                                    commodityOrigin,
                                    commodityFactoryTime,
                                    commodityShelf,
                                    commoditySpecialMsg,
                                    commodityPrice,
                                    commodityImages);
        boolean flag = cs.insertCommodity(c);
        response.getWriter().write(String.valueOf(flag));
    }

    //修改商品
    protected void updateCommodity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("commodityId");
        Integer commodityId = null;
        if(id != null) {
            commodityId = Integer.parseInt(id);
        }
        String commodityName = request.getParameter("commodityName");
        String commodityType = request.getParameter("commodityType");
        String commodityMaterial = request.getParameter("commodityMaterial");
        String commodityWeightStandard = request.getParameter("commodityWeightStandard");
        String commodityStandard = request.getParameter("commodityStandard");
        String commodityPackage = request.getParameter("commodityPackage");
        String commodityTaste = request.getParameter("commodityTaste");
        String commoditySpecial = request.getParameter("commoditySpecial");
        String commodityOrigin = request.getParameter("commodityOrigin");
        String commodityFactoryTime = request.getParameter("commodityFactoryTime");
        String commodityShelf = request.getParameter("commodityShelf");
        String commoditySpecialMsg = request.getParameter("commoditySpecialMsg");
        String price = request.getParameter("commodityPrice");
        Integer commodityPrice = null;
        if(price != null){
            commodityPrice = Integer.parseInt(price);
        }

        String commodityImages = request.getParameter("commodityImages");

        Commodity c = new Commodity(commodityId,
                                    commodityName,
                                    commodityType,
                                    commodityMaterial,
                                    commodityWeightStandard,
                                    commodityStandard,
                                    commodityPackage,
                                    commodityTaste,
                                    commoditySpecial,
                                    commodityOrigin,
                                    commodityFactoryTime,
                                    commodityShelf,
                                    commoditySpecialMsg,
                                    commodityPrice,
                                    commodityImages);

        boolean flag = cs.updateBySelective(c);
        response.getWriter().write(String.valueOf(flag));
    }

    protected void getCommodityById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String sid = request.getParameter("commodityId");
        Integer commodityId = null;
        if(sid != null){
            commodityId = Integer.parseInt(sid);
        }
        Commodity commodity = cs.selectByPrimaryKey(commodityId);
        Gson json = new Gson();
        String toJson = json.toJson(commodity);
        response.getWriter().write(toJson);
    }

}
